package cn.tkb.user.service.impl;

import cn.tkb.basic.exception.BusinessException;
import cn.tkb.basic.service.impl.BaseServiceImpl;
import cn.tkb.basic.util.MD5Utils;
import cn.tkb.basic.util.StrUtils;
import cn.tkb.user.constant.UserConstants;
import cn.tkb.user.domain.LoginInfo;
import cn.tkb.user.domain.User;
import cn.tkb.user.dto.UserDto;
import cn.tkb.user.mapper.LoginInfoMapper;
import cn.tkb.user.mapper.UserMapper;
import cn.tkb.user.service.ILoginInfoService;
import cn.tkb.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User>implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    @Transactional
    public void RegisterByphone(UserDto userDto) {
        //校验
        String phone = userDto.getPhone();
        //1.空校验
        if(StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPwd())
                || StringUtils.isEmpty(userDto.getVerifyCode())){
            throw new BusinessException("请填写必填项！！！");
        }
        //2.两次密码是否一致
        if(!userDto.getPassword().equalsIgnoreCase(userDto.getConfirmPwd())){
            throw new BusinessException("两次密码输入不一致请重新设置密码！！");
        }
        //3.验证码是否有效
        //获取验证码这里面是code+时间戳
        Object codeTmp = redisTemplate.opsForValue().get(UserConstants.BUSINESS_REGISTER_PREFIX + phone);
        if(codeTmp ==null){
            throw new BusinessException("请获取验证码！！！");
        }
        //4.验证码是否一致
        //获取验证码
        String code = codeTmp.toString().split(":")[0];
        if(!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码不一致请输入正确的验证码");
        }
        //5.判断该手机号是否被注册了
        User user = userMapper.loadByPhone(phone);
        if(user!=null){
            throw new BusinessException("该用户已被注册了，请直接登录");
        }
        //6.注册成功保存数据
        //6.1先保存loginInfo表然后再保存user表
        //由于前端传递过来的是userDto所以要转换成loginInfo对象
        LoginInfo loginInfo =userDto2loginInfo(userDto);
        loginInfoMapper.save(loginInfo);//返回自增id
        //因为loginIn里面的信息和user里面的信息有重复的直接用loginInfo转
        User info2user =LoginInfo2user(loginInfo);
        info2user.setLogininfo_id(loginInfo.getId());
        userMapper.save(info2user);
    }

    private User LoginInfo2user(LoginInfo loginInfo) {
        User info2user = new User();
        //按照同名属性值进行拷贝
        BeanUtils.copyProperties(loginInfo,info2user);
        return info2user;
    }
    //userDto转换成loginInfo对象
    private LoginInfo userDto2loginInfo(UserDto userDto) {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(userDto.getPhone());
        loginInfo.setPhone(userDto.getPhone());
        loginInfo.setType(0);
        //加盐
        String salt = StrUtils.getComplexRandomString(32);
        //加盐加密之后的密码
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        loginInfo.setSalt(salt);
        loginInfo.setPassword(password);
        return loginInfo;
    }
}
